<template>
  <div class="pf-student-manage">
    <div class="pf-mt-20 pf-text-left">
      <el-button icon="Refresh" @click="select"></el-button>
      <el-button type="primary" @click="create">创建</el-button>
      <el-button @click="del">删除</el-button>
    </div>
    <pf-table class="pf-mt-20" :config="tableConfig" :data="tableData">
      <el-table-column label="操作" fixed="right" width="80">
        <template #default="scope">
          <el-tooltip effect="light" placement="left" trigger="hover">
            <component :is="'Menu'" class="el-button--text" style="width: 18px"></component>
            <template #content>
              <div class="pf-flex pf-flex-column pf-flex-0">
                <el-button @click="handleClick(scope.row, 'edit')" link type="primary" size="mini"
                  >编辑</el-button
                >
              </div>
            </template>
          </el-tooltip>
        </template>
      </el-table-column>
    </pf-table>
    <el-dialog title="学员信息" v-model="show">
      <pf-form ref="formDom" :config="formConfig" :model="formData" />
      <template #footer>
        <div class="pf-text-right">
          <el-button type="primary" @click="confirmCreate">确认</el-button>
          <el-button @click="show = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, Ref } from 'vue';
import { useNotice } from '@/components/element-plus/notice';
import { emptyForm, emptyTable, FormModel, SysDict, TableModel, useData } from 'pf-component';
import { TableNameEnum } from '@/constants/enum/table-name.enum';
import { FormNameEnum } from '@/constants/enum/form-name.enum';
import { useStudent } from '@/views/org/student/useStudent';
import { useClass } from '@/views/org/class/useClass';
import { useOrg } from '@/views/system/org/use-org';

export default defineComponent({
  name: 'Student',
  setup() {
    const { message } = useNotice();
    const tableConfig: Ref<TableModel> = ref(emptyTable);
    const tableData: Ref<any[]> = ref([]);

    const show: Ref<boolean> = ref(false);
    const formConfig: Ref<FormModel> = ref(emptyForm);
    const formDom = ref(null);
    const formData: Ref = ref(null as any);
    onMounted(() => {
      Promise.all([
        useData().loadTable([{ name: TableNameEnum.dcStudent, config: tableConfig }]),
        useData().loadForm([{ name: FormNameEnum.dcStudent, config: formConfig, info: formData }])
      ]).then(ress => {
        if (ress.findIndex(res => !res) === -1) {
          initOption();
          select();
        }
      });
    });
    const initOption = () => {
      useOrg()
        .companyList()
        .then(res => {
          formConfig.value.setOptions(
            'comId',
            res.map(r => {
              return { key: r.comId, value: r.comName };
            })
          );
        });
      useClass()
        .list()
        .then(res => {
          formConfig.value.setOptions(
            'classId',
            res.map(r => {
              return {
                key: r.id,
                value: r.name
              };
            })
          );
        });
    };
    const { list, update } = useStudent();
    const select = () => {
      list().then(res => {
        tableData.value = res;
      });
    };

    const create = () => {
      formData.value = useData().toFormValue(formConfig.value);
      show.value = true;
    };
    const confirmCreate = () => {
      (formDom.value as any).validate((val: boolean) => {
        if (val) {
          update(formData.value).then(res => {
            if (res) {
              show.value = false;
              select();
            }
          });
        }
      });
    };
    const del = () => {
      message.error('待完善');
    };
    const handleClick = (data: SysDict, cmd: string) => {
      switch (cmd) {
        case 'edit':
          formData.value = data;
          show.value = true;
          break;
        default:
          message.error('未定义的操作！');
      }
    };

    return {
      select,
      tableData,
      tableConfig,
      handleClick,
      create,
      confirmCreate,
      del,
      show,
      formDom,
      formConfig,
      formData
    };
  }
});
</script>
<style scoped lang="scss">
.#{$prefix} {
  &-student-manage {
    //margin: 10px;
    //padding: 0;
    overflow-y: auto;
    height: calc(100% - 60px);
  }
}
</style>
